package com.example.oom;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class UnsafeOOMDemo {
    public static void main(String[] args) throws Exception {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);

        List<Long> addresses = new ArrayList<>();
        try {
            while (true) {
                long address = unsafe.allocateMemory(10 * 1024 * 1024); // 10MB
                addresses.add(address);
                System.out.println("Allocated 10MB off-heap");
            }
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }
}
